package de.gee.erep.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

import de.gee.erep.client.ui.presenter.CitizenPresenter;
import de.gee.erep.client.ui.presenter.CountryPresenter;
import de.gee.erep.client.ui.presenter.HomePresenter;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AnotherErepublikTool implements EntryPoint,
		ValueChangeHandler<String> {
	/***/
	private final HandlerManager eventBus = new HandlerManager(this);
	/***/
	private Environment environment;

	/**
	 * This is the entry point method.
	 */
	public final void onModuleLoad() {
		setLaunchBar(RootPanel.get("nav"));
		setEnvironment(new Environment(new Model(), History.getToken()));
		/*
		 * Set up the history management, and start by showing the login form.
		 */
		History.addValueChangeHandler(this);
		environment.launch(HomePresenter.PLACE, RootPanel.get("con"), eventBus);
	}

	/**
	 * @param panel
	 *            p
	 * 
	 */
	private void setLaunchBar(final Panel panel) {
		final HorizontalPanel hp = new HorizontalPanel();
		final Button homeBt = new Button("home");
		final Button countries = new Button("Country-Stats");
		final Button citizens = new Button("Citizens");
		homeBt.addStyleName("navButton");
		homeBt.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				environment.launch(HomePresenter.PLACE, RootPanel.get("con"), eventBus);
			}
		});
		countries.addStyleName("navButton");
		countries.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				environment.launch(CountryPresenter.PLACE, RootPanel.get("con"), eventBus);
			}
		});
		citizens.addStyleName("navButton");
		citizens.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(final ClickEvent event) {
				environment.launch(CitizenPresenter.PLACE, RootPanel.get("con"), eventBus);
			}
		});
		// final Button
		hp.add(homeBt);
		hp.add(countries);
		hp.add(citizens);
		RootPanel.get("nav").add(hp);
	}

	/**
	 * @return the eventBus
	 */
	public final HandlerManager getEventBus() {
		return eventBus;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(
	 * com.google. gwt.event.logical.shared.ValueChangeEvent)
	 */
	@Override
	public final void onValueChange(final ValueChangeEvent<String> event) {
		environment.launch(event.getValue(), eventBus);
	}

	/**
	 * @param environment
	 *            the environment to set
	 */
	public final void setEnvironment(final Environment environment) {
		this.environment = environment;
	}

	/**
	 * @return the environment
	 */
	public final Environment getEnvironment() {
		return environment;
	}
}
